﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;

namespace HY.WEB.SA
{
    public partial class company_town : PageBase
    {

        public string whe = "",stype="";
        public string us,um,bh;
        public string tab = "company";

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            bh = Session["bh"].ToString();
            um = Session["user"].ToString();

           // whe = " cbh in (select cbh from company_fav where sbh='" + bh + "') and cbh not in (select cbh from company_del where sbh='" + bh + "')";

            if (!IsPostBack)
            {          

                btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");


                //试用开关，50条记录 
                if ((bool)Session["test"] & BLL.Bllbase.check_rec(tab))
                {
                    Alert.Show("试用版系统，只能输入50条记录");
                }
                else
                {
                    btnNew.OnClientClick = Window1.GetSaveStateReference(hidstype.ClientID)
                                       +Window1.GetShowReference("comp_add.aspx", "新增");
                }
               
 


               // btnSearch.OnClientClick =  Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
      
              //  BindGrid();
                fill(); //填充查询
                
            }

            if (stype == "") stype= hidstype.Text;
        }


        //取城市
        public static string getcity(string val)
        {
            DataTable dte = BLL.Bllbase.get_table("[pub_area]"," scode='"+val+"' ");
            if (dte.Rows.Count > 0)
                return dte.Rows[0]["svalue"].ToString();
            else
                return "";

        }


        //检查是否有图标
        public static string checkico(string val)
        {
            DataTable dte = BLL.Bllbase.get_table("[company_fav]", "cbh='" + val + "'");
            if (dte.Rows.Count > 0)
                return "../icon/lock.png";
            else
                return "../icon/user.png";

        }

        //检查客户级别
        public static string hightico(string val)
        {
            string lev = "0";

            DataTable dte = BLL.Bllbase.get_table("company", "cbh='" + val + "'");
            if (dte.Rows.Count > 0)
            {
                lev = dte.Rows[0]["slevel"].ToString().Trim();
                switch (lev)
                {
                    case "100":
                        return "../icon/award_star_bronze_1.png";
                        break;
                    case "90":
                        return "../icon/award_star_gold_1.png";
                        break;
                    case "80":
                        return "../icon/award_star_silver_1.png";
                        break;
                    default:
                        return "../icon/award_star_silver_3.png";
                        break;

                }
            }
            else
                return "../icon/award_star_silver_3.png";

        }
        public static string highttip(string val)
        {
            string lev = "0";

            DataTable dte = BLL.Bllbase.get_table("company", "cbh='" + val + "'");
            if (dte.Rows.Count > 0)
            {
                lev = dte.Rows[0]["slevel"].ToString().Trim();
                switch (lev)
                {
                    case "100":
                        return "重点客户";
                        break;
                    case "90":
                        return "潜在客户";
                        break;
                    case "80":
                        return "一般客户";
                        break;
                    default:
                        return "普通客户";
                        break;

                }
            }
            else
                return "普通客户";

        }

          private void fill()
          {
             // DataTable d = BLL.BLLfun.get_search(tab);
              ddlfiled.Items.Clear();

              DataTable d = BLL.Bllbase.get_table("pub_zone", "", "order by svalue desc");
             for (int i = 0; i < d.Rows.Count; i++)
                    {
                        ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][2].ToString().Replace("镇", ""), d.Rows[i][2].ToString().Replace("镇", "")));
                    }
               ddlfiled.DataBind();
               ddlfiled.Items.Add(new ExtAspNet.ListItem("", ""));
               ddlfiled.SelectedIndex = ddlfiled.Items.IndexOf(ddlfiled.Items.FindByValue(""));
           
              
              // DataTable e = BLL.BLLfun.get_where();
              // for (int i = 0; i < e.Rows.Count; i++)
              // {
              //     ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
              // }
              //  ddlwhere.DataBind(); 
              //ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));

            
        }



        private void BindGrid()
        {

            DataTable table = BLL.Bllbase.get_table(tab, whe,"order by sid desc");

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", " sid ", " desc");

            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = BLL.Bllbase.get_table(tab, whe);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }



        

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row+val][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了取消收藏项：" + sb.ToString().TrimEnd(','));
           // HY.BLL.Bllbase.DeleteList(tab,sb.ToString().TrimEnd(','));
            BLL.BllSales.deletelistsalse(tab,sb.ToString().TrimEnd(','));
            BindGrid();
        }

        protected void btnDelete2_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row + val][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了删除到回收站：" + sb.ToString().TrimEnd(','));
            BLL.BllSales.deletelistsalse2(tab, sb.ToString().TrimEnd(','));
            BindGrid();
        }


        /// <summary>
        /// 直接删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete3_Click(object sender, EventArgs e)
        {
            //int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));

            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));

            BindGrid();
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = ddlfiled.SelectedValue;// txt.Text.Trim();
              stype = hidstype.Text;
              try
              {
              if (key == ""&stype!="") 
                  whe = whe+" and "+ BLL.pub.pubwhere.get_where_sql(us,tab);

               BindGrid();


              }catch(Exception ex)
              {
                 // Alert.Show("查询条件错误");
                  whe = " cbh in (select cbh from company_fav where sbh='" + bh + "') and cbh not in (select cbh from company_del where sbh='" + bh + "')";
              }
              finally
              {
                  whe = " cbh in (select cbh from company_fav where sbh='" + bh + "') and cbh not in (select cbh from company_del where sbh='" + bh + "')";
              }

              BindGrid();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
           // txt.ShowTrigger1 = true;

            string ddlf = ddlfiled.SelectedValue.Trim();

            if (ddlf == "") return;
            // string ddlw = ddlwhere.SelectedValue;
            string key = ddlf;// txt.Text.Trim();
            //if (ddlw.Equals("="))
                whe = whe+" and addresss like  '%" + key + "%'";
            //else
            //{
            //  if (ddlf=="sid"||ddlf=="编号") 
            //      whe = whe+" and "+ "" + ddlf + " " + ddlw + " " + key + "";
            //  else
            //      whe =  whe+" and "+"" + ddlf + " " + ddlw + " '" + key + "'";

            //}
            //if (key == "") whe = " cbh in (select cbh from company_fav where sbh='" + bh + "') and cbh not in (select cbh from company_del where sbh='" + bh + "')";

            BindGrid();

        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            //txt.Text = String.Empty;
            //txt.ShowTrigger1 = false;
            BindGrid();
        }

      
    

        }




    
}